package arithmetic.LeetCode.Majority_Element;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author zhangyanqi
 * @since 1.0 2017/10/17
 */
public class Solution {

    public int majorityElement(int[] nums) {

        Map<Integer, Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            Integer integer = map.get(num);
            if (integer == null) {
                map.put(num, 1);
            } else {
                map.put(num,integer + 1);
            }
        }
        int maxTime = 0;
        int result = 0;
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Integer> next = iterator.next();
            Integer value = next.getValue();
            if (value >= maxTime) {
                maxTime = value;
                result = next.getKey();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] o = {2,2};
        int i = new Solution().majorityElement(o);
        System.out.println(i);
    }
}
